package com.itheima.service;


import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.constant.MessageConstant;
import com.itheima.dao.PermissionMapper;
import com.itheima.pojo.PageResult;
import com.itheima.pojo.Permission;
import com.itheima.pojo.QueryPageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service(interfaceClass = PermissionService.class)
@Transactional
public class PermissionServicelmpl implements PermissionService {

    @Autowired
    PermissionMapper permissionMapper;

    /**
     * 添加新的权限
     * @param permission
     */
    @Override
    public void add(Permission permission) {
         permissionMapper.add(permission);
    }

    /**
     * 分页查询总条数在页面显示出来
     * @param queryPageBean
     * @return
     */
    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        Integer pageSize = queryPageBean.getPageSize();
        Integer currentPage = queryPageBean.getCurrentPage();
        String queryString = queryPageBean.getQueryString();
        PageHelper.startPage(currentPage, pageSize);
        Page<Permission> page = permissionMapper.findPage(queryString);
        List<Permission> result = page.getResult();
        long total = page.getTotal();
        return new PageResult(total, result);
    }

    /**
     *
     *根据id查询权限 用于编辑时的数据回显
     * @param
     * @return
     */
    @Override
    public Permission findById(Integer id) {
          return permissionMapper.findById(id);

    }

    //编辑权限

    /**
     * 编辑权限
     * @param permission
     */
    @Override
    public void edit(Permission permission) {
        permissionMapper.edit(permission);
    }

    /**
     * 根据权限id查询角色和权限的关系删除权限
     * @param id
     */
    @Override
    public void delete(int id) {
        permissionMapper.delete(id);
       /* //根据权限id  查询权限和角色的关系表
        Long count =  permissionMapper.delete(id);
        //判断该权限是否已经赋予了角色
        if (count>0){
            //已经被赋予角色  不可被删除
            throw new RuntimeException(MessageConstant.DELETE_PERMISSION_FAIL);
        }
        //如果没有赋予角色  则可以删除
         permissionMapper.delete(id);*/
    }
}
